package org.leviatan.definator.gui.swingcomponents;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JPanel;
import javax.swing.JTable;

import org.leviatan.definator.core.model.Field;
import org.leviatan.definator.core.model.Record;
import org.leviatan.definator.core.model.Table;
import org.leviatan.definator.gui.RuntimeContext;

public class RightEditViewPane extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private JPanel singleEditViewPanel = null;
	
	private SingleTableEditView tableEditPanel = null;
	private SingleRecordEditView recordEditPanel = null;
	private SingleFieldEditView fieldEditPanel = null;
	
	private NewTableView newTableView = null;
	private NewRecordView newRecordView = null;
	private NewFieldView newFieldView = null;
	
	private JTable jTable = null;
	 

	/**
	 * This is the default constructor
	 */
	public RightEditViewPane() {
		super();
		initialize();
	}
	
	public void displaySingleEditRecordPanel(Record recMdl) {
		this.recordEditPanel.setModel(recMdl);
		this.tableEditPanel.setVisible(false);
		this.tableEditPanel.setEnabled(false);
		this.recordEditPanel.setVisible(true);
		this.recordEditPanel.setEnabled(true);
		this.fieldEditPanel.setVisible(false);
		this.fieldEditPanel.setEnabled(false);
		
		this.newTableView.setEnabled(false);
		this.newTableView.setVisible(false);
		this.newRecordView.setEnabled(false);
		this.newRecordView.setVisible(false);
		this.newFieldView.setEnabled(false);
		this.newFieldView.setVisible(false);
		
	}
	
	public void displaySingleEditFieldPanel(Field fldMdl) {
		this.fieldEditPanel.setModel(fldMdl);
		this.tableEditPanel.setVisible(false);
		this.tableEditPanel.setEnabled(false);
		this.recordEditPanel.setVisible(false);
		this.recordEditPanel.setEnabled(false);
		this.fieldEditPanel.setVisible(true);
		this.fieldEditPanel.setEnabled(true);
		
		this.newTableView.setEnabled(false);
		this.newTableView.setVisible(false);
		this.newRecordView.setEnabled(false);
		this.newRecordView.setVisible(false);
		this.newFieldView.setEnabled(false);
		this.newFieldView.setVisible(false);
		

	}
	
	public void displaySingleEditTablePanel(Table tblMdl) {
		this.tableEditPanel.setModel(tblMdl);
		this.tableEditPanel.setVisible(true);
		this.tableEditPanel.setEnabled(true);
		this.recordEditPanel.setVisible(false);
		this.recordEditPanel.setEnabled(false);
		this.fieldEditPanel.setVisible(false);
		this.fieldEditPanel.setEnabled(false);
		
		this.newTableView.setEnabled(false);
		this.newTableView.setVisible(false);
		this.newRecordView.setEnabled(false);
		this.newRecordView.setVisible(false);
		this.newFieldView.setEnabled(false);
		this.newFieldView.setVisible(false);
			
	}
	
	public void displayNewTablePanel() {
		this.tableEditPanel.setVisible(false);
		this.tableEditPanel.setEnabled(false);
		this.recordEditPanel.setVisible(false);
		this.recordEditPanel.setEnabled(false);
		this.fieldEditPanel.setVisible(false);
		this.fieldEditPanel.setEnabled(false);
		
		this.newTableView.setEnabled(true);
		this.newTableView.setVisible(true);
		this.newRecordView.setEnabled(false);
		this.newRecordView.setVisible(false);
		this.newFieldView.setEnabled(false);
		this.newFieldView.setVisible(false);
		
	}
	
	public void displayNewRecordPanel(String tblId) {
		this.tableEditPanel.setVisible(false);
		this.tableEditPanel.setEnabled(false);
		this.recordEditPanel.setVisible(false);
		this.recordEditPanel.setEnabled(false);
		this.fieldEditPanel.setVisible(false);
		this.fieldEditPanel.setEnabled(false);
		
		this.newTableView.setEnabled(false);
		this.newTableView.setVisible(false);
		this.newRecordView.setEnabled(true);
		this.newRecordView.setVisible(true);
		this.newFieldView.setEnabled(false);
		this.newFieldView.setVisible(false);
		
		this.newRecordView.setTblCmbChoices(tblId);
		
	}
	
	public void displayNewFieldPanel(String recId) {
		this.tableEditPanel.setVisible(false);
		this.tableEditPanel.setEnabled(false);
		this.recordEditPanel.setVisible(false);
		this.recordEditPanel.setEnabled(false);
		this.fieldEditPanel.setVisible(false);
		this.fieldEditPanel.setEnabled(false);
		
		this.newTableView.setEnabled(false);
		this.newTableView.setVisible(false);
		this.newRecordView.setEnabled(false);
		this.newRecordView.setVisible(false);
		this.newFieldView.setEnabled(true);
		this.newFieldView.setVisible(true);
		
		this.newFieldView.setRecCmbChoices(recId);
		
	}
	

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		//GridBagConstraints gridBagConstraints = new GridBagConstraints();
		this.setLayout(new GridBagLayout());
		
		this.tableEditPanel = new SingleTableEditView();
		this.recordEditPanel = new SingleRecordEditView();
		this.fieldEditPanel = new SingleFieldEditView();
		this.newTableView = new NewTableView();
		this.newRecordView = new NewRecordView();
		this.newFieldView = new NewFieldView();
		
		this.newTableView.setVisible(false);
		this.newTableView.setEnabled(false);
		this.newRecordView.setVisible(false);
		this.newRecordView.setEnabled(false);
		this.newFieldView.setVisible(false);
		this.newFieldView.setEnabled(false);
		
		this.tableEditPanel.setVisible(false);
		this.tableEditPanel.setEnabled(false);
		this.recordEditPanel.setVisible(false);
		this.recordEditPanel.setEnabled(false);
		this.fieldEditPanel.setVisible(false);
		this.fieldEditPanel.setEnabled(false);

		
		this.jTable = new JTable();
		this.singleEditViewPanel = new JPanel();
		RuntimeContext.jTable = this.jTable;
		RuntimeContext.singleEditViewPanel = this;
		
		
		GridBagConstraints top = new GridBagConstraints();
		GridBagConstraints header = new GridBagConstraints();
		GridBagConstraints bottom = new GridBagConstraints();
		
		top.anchor = GridBagConstraints.NORTHWEST;
		top.gridx = 0;
		top.gridy = 0;
		top.insets = new Insets(5, 5, 5, 5);
		//top.weightx = 1.0;
		//top.weighty = 1.0;
		
		header.anchor = GridBagConstraints.NORTHWEST;
		header.gridx = 0;
		header.gridy = 1;
		//header.weightx = 1.0;
		//header.weighty = 1.0;
		header.fill = GridBagConstraints.BOTH;
		header.insets = new Insets(10, 5, 0, 5);
		
		
		bottom.anchor = GridBagConstraints.NORTHWEST;
		bottom.gridx = 0;
		bottom.gridy = 2;
		bottom.weightx = 1.0;
		bottom.weighty = 1.0;
		bottom.fill = GridBagConstraints.BOTH;
		bottom.insets = new Insets(0, 5, 0, 5);
		
		
		this.add(this.singleEditViewPanel, top);
		this.add(tableEditPanel, top);
		this.add(recordEditPanel, top);
		this.add(fieldEditPanel, top);
		this.add(newTableView, top);
		this.add(newRecordView, top);
		this.add(newFieldView, top);
		
		this.add(jTable.getTableHeader(), header);
		this.add(jTable, bottom);
		//container.setLayout(new BorderLayout());
		//container.add(table.getTableHeader(), BorderLayout.PAGE_START);
		//container.add(table, BorderLayout.CENTER);

	}

}
